local IsClient = game:GetService("RunService"):IsClient();
local M={}
local gBuffGui=nil
if IsClient then
     M = _G.ClientOHOH.class.new(script, _G.ClientOHOH.files.Client_PlayerComponent());

     gBuffGui= _G.ClientOHOH.load.UIManager():CreateUI("BuffGui",require(script.BuffGui))

else
    M = _G.ServerOHOH.class.new(script,  _G.ServerOHOH.files.Server_PlayerComponent());
end

local Buff_Cfg=_G.CommonOHOH.config.Buff
local ItemTool=_G.CommonOHOH.load.ItemTool();

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

    self.BuffList={} --buff 列表{[Buff对象]=true}
    self.BuffClassList={} --buff类名{[类名]=buff对象}
    
    if IsClient then
        self:ClientInit()
    else
        self:ServerInit()
    end 
end;

function M.ServerInit(self)

   self.DataStore=self.parent:GetComponent(_G.ServerOHOH.load.Server_PlayerDataComponent())
   for id,time in pairs( self.DataStore:GetData("usingBuff").__real) do
        -- print("ServerInit--->:",id,time)
        self:ServerCreateBuff(id,time)
    end
end

function M.ClientInit(self)

    self.DataStore=self.parent:GetComponent(_G.ClientOHOH.load.Client_PlayerDataComponent())
    for id,time in pairs( self.DataStore:GetData("usingBuff")) do
        -- print("ClientInit--->:",id,time)

        self:CreateBuffByClient(id,time)
    end

end


function M.CreateBuff(self,buffId,timeLeft) -- 已经存在的buff，直接加时间

    local cfg=Buff_Cfg[buffId]

    assert(cfg,"cfg==nil,id:"..buffId)

    timeLeft=timeLeft or cfg.Time

    local buff=self.BuffClassList[cfg.ClassName]
    if buff then --已经存在buff，直接加时间
        buff:Add(timeLeft,unpack(cfg.Arg))
    else
        local buffClass= require(script:WaitForChild(cfg.ClassName))
        buff= buffClass.new(buffId,self.parent,timeLeft,unpack(cfg.Arg))
        buff.DestroyEvent:Connect(function(obj)
            self.BuffList[obj]=nil
            self.BuffClassList[cfg.ClassName]=nil
        end)

        self.BuffList[buff]=true
        self.BuffClassList[cfg.ClassName]=buff
    end

    return buff
end


function M.CreateBuffByClient(self,buffId,timeLeft) 
    local buff=self:CreateBuff(buffId,timeLeft)

    local guiCell=gBuffGui:CreatCellShow(buff:GetId(),buff:TimeLeft())

    buff.UpdateEvent:Connect(function(t)
        guiCell:Update(t)
    end)

    buff.DestroyEvent:Connect(function(t)
        guiCell:dtor()
    end)
end


function M.ClientCreateBuff(self,buffId,timeLeft) 


    if self.parent:Invoke(_G.ClientOHOH.load.FunctionList().use_buff,buffId) then

        self:CreateBuffByClient(buffId,timeLeft)

        return  true
    end
    return  false
end


function M.ServerCreateBuff(self,buffId,timeLeft)
    local count= ItemTool.GetItemCount(buffId,self.DataStore)
    if count and count>0 then
        local buff=self:CreateBuff(buffId,timeLeft)

        ItemTool.SaveData(buffId,self.DataStore,-1)	
        self.DataStore:Execut(function()
        end);	

        buff.UpdateEvent:Connect(function(t)
            local id=buff:GetId()
            local leftTime=buff:TimeLeft()
           local usingBuff= self.DataStore:GetData("usingBuff")
           usingBuff.__real[id]=leftTime

        end)

        buff.DestroyEvent:Connect(function(t)
            local id=buff:GetId()
            local usingBuff= self.DataStore:GetData("usingBuff")
            usingBuff.__real[id]=nil
        end)
        self.parent:SynchDataToClient("buff","usingBuff")

        return true
    end
    return false
end

function M.dtor(self)
    for k,v in pairs(self.BuffList) do
        k:dtor()
    end
	M.super.dtor(self);
end;

if not IsClient then
    local function_list = _G.ServerOHOH.function_list;
    M:RegistFunction(function_list.use_buff,M.ServerCreateBuff)
end

return M